package com.gohome.gohomechat.ui.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.gohome.gohomechat.GoHomeHelper;
import com.gohome.gohomechat.GoHomeModel;
import com.gohome.gohomechat.R;
import com.gohome.gohomechat.db.GoHomeDBTools;
import com.gohome.gohomechat.model.entify.CurrentUser;
import com.gohome.gohomechat.presenter.EditorPresenter;
import com.gohome.gohomechat.presenter.callback.EditorCallBack;
import com.gohome.gohomechat.presenter.presenterlistener.EditorPresenterListener;
import com.gohome.gohomechat.ui.viewlistener.EditorViewInterface;
import com.gohome.gohomechat.utils.ToActivityUtil;
import com.gohome.gohomechat.utils.ToastUtil;
import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.SaveCallback;
import com.squareup.picasso.Picasso;

/**
 * Created by dllo on 16/3/9.
 */
public class EditorDataActivity extends BaseActivity implements EditorViewInterface, View.OnClickListener {
    private TextView editor_replace, editor_title_save;
    private EditText editor_nick, editor_singnature, editor_birthday, editor_sex, editor_age, editor_zodiaz, editor_boold, editor_email;
    private EditorPresenterListener editorPresenterListener;
    private ImageButton editor_imgbtn_back;
    //  private byte[] headImgByte;
    private ImageView editor_head;

    @Override
    protected int getLayout() {
        return R.layout.activity_editordata;
    }

    @Override
    protected void initPresenter() {
        editorPresenterListener = new EditorPresenter(this);
    }

    @Override
    protected void initView() {


        editor_head = findView(R.id.editor_head);
        editor_replace = findView(R.id.editor_replace);
        editor_nick = findView(R.id.editor_nick);
        editor_singnature = findView(R.id.editor_singnature);
        editor_birthday = findView(R.id.editor_birthday);
        editor_sex = findView(R.id.editor_sex);
        editor_age = findView(R.id.editor_age);
        editor_zodiaz = findView(R.id.editor_zodiaz);
        editor_boold = findView(R.id.editor_boold);
        editor_email = findView(R.id.editor_email);
        editor_imgbtn_back = findView(R.id.editor_imgbtn_back);
        editor_title_save = findView(R.id.editor_title_save);

        //   headImgByte = getIntent().getByteArrayExtra("bitmap");
        if (GoHomeModel.getInstance().getHeadByte() != null) {
            Log.i("wcl", "更换头像");
            Bitmap bitmap = BitmapFactory.decodeByteArray(GoHomeModel.getInstance().getHeadByte(), 0, GoHomeModel.getInstance().getHeadByte().length);

            editor_head.setImageBitmap(bitmap);
        }
    }

    @Override
    protected void initData() {
        editor_imgbtn_back.setOnClickListener(this);
        editor_title_save.setOnClickListener(this);
        editor_replace.setOnClickListener(this);
    }

    @Override
    public void showDialog() {
        showProgressDialog();
    }

    @Override
    public void dismissDialog() {
        dismissDialog();
    }

    @Override
    public void saveSuccess() {
        ToastUtil.showToast("资料保存成功");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.editor_replace: // 更换头像
                ToActivityUtil.startToActivity(this, DisPlayImageActivity.class);

                break;
            case R.id.editor_imgbtn_back:
                finish();
                break;
            case R.id.editor_title_save:

                CurrentUser user = new CurrentUser();
                user.setNick(editor_nick.getText().toString());
                user.setSingnature(editor_singnature.getText().toString());
                user.setBirthday(editor_birthday.getText().toString());
                user.setSex(editor_sex.getText().toString());
                user.setAge(editor_age.getText().toString());
                user.setZodiaz(editor_zodiaz.getText().toString());
                user.setBoold(editor_boold.getText().toString());
                user.setEmail(editor_email.getText().toString());
                user.setHead(GoHomeModel.getInstance().getHead());
                // editorPresenterListener.saveData(user);
                save(user);
                break;
        }
    }


    public void save(final CurrentUser user) {
        ParseQuery<ParseObject> query = ParseQuery.getQuery("PersonalDataGoHome");
        Log.i("wcl", GoHomeModel.getInstance().getCurrentUserName() + " 登录用户 判断");
        query.whereEqualTo("username", GoHomeModel.getInstance().getCurrentUserName());
        query.getFirstInBackground(new GetCallback<ParseObject>() {
            @Override
            public void done(ParseObject object, ParseException e) {
                if (e == null) {
                    Log.i("wcl", object.getObjectId() + " 乱码id");
                    ParseQuery<ParseObject> query1 = ParseQuery.getQuery("PersonalDataGoHome");
                    query1.getInBackground(object.getObjectId(), new GetCallback<ParseObject>() {
                        @Override
                        public void done(final ParseObject object, ParseException e) {
                            if (e == null) {
                                //  object.put("username", personal_username.getText().toString().trim());
                                Log.i("wcl", user.getSex() + " 修改");
                                object.put("sex", user.getSex());
                                object.put("zodiaz", user.getZodiaz());
                                object.put("boold", user.getBoold());
                                object.put("age", user.getAge());
                                object.put("singnature", user.getSingnature());
                                object.put("birthday", user.getBirthday());
                                object.put("nick_name", user.getNick());
                                object.put("email", user.getEmail());
                                if (GoHomeModel.getInstance().getHeadByte() != null) {
                                    Log.i("wcl", GoHomeModel.getInstance().getHeadByte().length + " 图片保存");
                                    final ParseFile parseFile = new ParseFile(GoHomeModel.getInstance().getCurrentUserName() + "head.png", GoHomeModel.getInstance().getHeadByte());
                                    parseFile.saveInBackground(new SaveCallback() {
                                        @Override
                                        public void done(ParseException e) {
                                            if (e == null) {
                                                Log.i("wcl", "图片保存完成");
                                                object.put("head_img", parseFile);
                                                object.saveInBackground(new SaveCallback() {
                                                    @Override
                                                    public void done(ParseException e) {
                                                        if (e == null) {
                                                            Log.i("wcl", "数据保存完成");
                                                            Toast.makeText(EditorDataActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                                                        } else
                                                            Log.i("wcl", e.getMessage() + "更改数据");
                                                    }
                                                });
                                            }
                                        }
                                    });
                                }
                                Log.i("wcl", "即将数据保存");

                            } else {
                                Log.i("wcl", e.getMessage() + "  objectId");

                            }
                        }
                    });
                } else {
                    Log.i("wcl", e.getMessage() + "  username");

                }
            }
        });
        ParseQuery<ParseObject> allUserGoHome = ParseQuery.getQuery("AllUserGoHome");
        allUserGoHome.whereEqualTo("username", GoHomeModel.getInstance().getCurrentUserName());
        allUserGoHome.getFirstInBackground(new GetCallback<ParseObject>() {
            @Override
            public void done(ParseObject object, ParseException e) {
                if (e == null) {
                    ParseQuery<ParseObject> objectParseQuery = new ParseQuery<ParseObject>("AllUserGoHome");
                    objectParseQuery.whereEqualTo("objectId", object.getObjectId());
                    objectParseQuery.getFirstInBackground(new GetCallback<ParseObject>() {
                        @Override
                        public void done(final ParseObject object, ParseException e) {
                            if (e == null) {
                                if (GoHomeModel.getInstance().getHeadByte() != null) {
                                    final ParseFile parseFile = new ParseFile(GoHomeModel.getInstance().getCurrentUserName() + "head.png", GoHomeModel.getInstance().getHeadByte());
                                    parseFile.saveInBackground(new SaveCallback() {
                                        @Override
                                        public void done(ParseException e) {
                                            if (e == null) {
                                                object.put("head_img", parseFile);
                                                object.saveInBackground();
                                                }
                                            }
                                        }
                                        );
                                    }
                                }
                            }
                        }
                        );
                    }
                }
            }
            );
            GoHomeDBTools.getInstance().deleteAllDatas_personal();
            GoHomeDBTools.getInstance().insert_personal(user);
        }

        @Override
    protected void onResume() {
        editor_head.setImageBitmap(GoHomeHelper.getInstance().getCurrentUserHead());
        super.onResume();
    }
}
